const imgExtArr = ["jpg", "jpeg", "gif", "bmp", "png", "tif", "tiff"];
const lrz = require("./lrz.js");
const { v4: uuidv4 } = require("uuid");
module.exports = {
    isImage: function (filename) {
        if (!filename)
            return false;
        let ext = filename.split(".").pop();
        return imgExtArr.includes(ext);
    },
    getUuid() {
        return uuidv4().replace(/\-/g, "");
    },
    //将base64转换为文件
    dataURLtoFile(dataurl, filename) {
        var arr = dataurl.split(","),
            mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]),
            n = bstr.length,
            u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new File([u8arr], filename, { type: mime });
    },
    //将base64转换为blob
    dataURLtoBlob: function (dataurl) {
        var arr = dataurl.split(','),
            mime = arr[0].match(/:(.*?);/)[1],
            bstr = atob(arr[1]),
            n = bstr.length,
            u8arr = new Uint8Array(n);
        while (n--) {
            u8arr[n] = bstr.charCodeAt(n);
        }
        return new Blob([u8arr], { type: mime });
    },
    //将blob转换为file
    blobToFile: function (theBlob, fileName) {
        theBlob.lastModifiedDate = new Date();
        theBlob.name = fileName;
        return theBlob;
    },
    //文件大小转化器
    fileSizeConvert(limit) {
        var size = "";
        if (limit < 0.1 * 1024) {                            //小于0.1KB，则转化成B
            size = limit.toFixed(2) + "B"
        } else if (limit < 0.1 * 1024 * 1024) {            //小于0.1MB，则转化成KB
            size = (limit / 1024).toFixed(2) + "KB"
        } else if (limit < 0.1 * 1024 * 1024 * 1024) {        //小于0.1GB，则转化成MB
            size = (limit / (1024 * 1024)).toFixed(2) + "MB"
        } else {                                            //其他转化成GB
            size = (limit / (1024 * 1024 * 1024)).toFixed(2) + "GB"
        }

        var sizeStr = size + "";                        //转成字符串
        var index = sizeStr.indexOf(".");                    //获取小数点处的索引
        var dou = sizeStr.substr(index + 1, 2)            //获取小数点后两位的值
        if (dou == "00") {                                //判断后两位是否为00，如果是则删除00                
            return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
        }
        return size;
    },
    //解析加载文件
    loaddingFile(file) {
        return new Promise((resolve, reject) => {
            let fileName = file.name;
            let resultObj = { file, fileName, fileId: this.getUuid(), newadd: true };
            if (!this.isImage(fileName)) {
                resultObj.fileLen = file.size;
                resultObj.file = file;
                resultObj.isImage = false;
                resolve(resultObj);
            }
            else {
                lrz(file, { quality: 0.7 }).then(rst => {
                    resultObj.fileLen = rst.fileLen;
                    resultObj.base64 = rst.base64;
                    resultObj.file = rst.file;//这个是经过压缩的,会小一点
                    resultObj.isImage = true;
                    resolve(resultObj);
                }).catch(err => reject(err));
            }
        });
    }
}